package com.googlecode.utils
{
	import com.googlecode.data.ProjectConstants;
	import com.googlecode.data.materials.FileAssetMaterialVO;
	
	import flash.filesystem.File;
	
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;

	public class ResolveFilePath extends Mediator
	{
		public static const NAME:String = "ResolveFilePath";
		
		protected static var _instance:ResolveFilePath;
		
		public function ResolveFilePath()
		{
			if(_instance)
			{
				throw(new Error("Do not instantiate. Use ResolveFilePath.instance()"));		
			}
		}
		
		public static function get instance():ResolveFilePath
		{
			if(_instance == null)
			{
				_instance = new ResolveFilePath();
			}
			
			return _instance;
		}
		
		public function getPath(fileAsset:FileAssetMaterialVO, savePath:String):String
		{
			if(!fileAsset.isRelative)
			{
				// must be an absolute path
				
				return fileAsset.filePath;
			}
			
			var length:uint = ProjectConstants.APPLICATION_ASSETS_DIRECTORY.length;

			if(fileAsset.filePath.substr(0, length) == ProjectConstants.APPLICATION_ASSETS_DIRECTORY)
			{
				// must be a default material

				return fileAsset.filePath;
			}

			if(savePath)
			{
				// must be from a saved project or a packaged project
				// must also be relative
				var saveFile:File = new File();
				
				saveFile.url = savePath;

				return saveFile.parent.url + "/" + fileAsset.filePath;
			}
			
			return null;
		}
		
	}
}